package com.matt.repository;

import com.matt.domain.LiveRoom;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2017/7/14
 * *******************************
 */

public interface LiveRoomRepository extends JpaRepository<LiveRoom, String> {
    List<LiveRoom> findByAreaIdAndStatus(String areaId, Integer status);

    List<LiveRoom> findByAreaIdAndStatusAndTeachingAreaIdIsNull(String areaId, Integer status);

    List<LiveRoom> findByAreaId(String areaId);

    List<LiveRoom> findByIdInAndStatus(String[] ids, Integer status);

    List<LiveRoom> findByTeachingAreaIdAndStatus(String teachingAreaId, Integer status);

    List<LiveRoom> findByTeachingAreaIdInAndStatus(List<String> teachingAreaIds, Integer status);

    LiveRoom findFirstByNameAndAreaId(String name, String areaId);

    LiveRoom findByIdAndAreaId(String id, String areaId);

//    LiveRoom findAllByYingshiIdAndAreaId(String yinshiId,String areaId);

    List<LiveRoom> findAllByDeviceSerialAndAreaIdOrderByChannelNo(String deviceSerial,String areaId);

    @Query(value = "select l from LiveRoom l where l.status = 1 and l.thumbUrl = null")
    List<LiveRoom> findByThumbUrlIsNull();

    List<LiveRoom> findByStatus(Integer status);


    LiveRoom findFirstByAreaIdAndDeviceSerialAndChannelNo(String areaId,String deviceSerial,Integer channelNo);

//    List<LiveRoom> findByYingshiId(String yinshiId);

//    @Query(value = "select l.* from liveRoom l left join teaching_area t on l.teachingAreaId = t.id where l.aid = ?1 and l.status = ?2 order by t.cameraSize desc limit ?3,?4", nativeQuery = true)
//    List<LiveRoom> findByAreaIdAndStatus(String areaId, Integer status, Integer begin, Integer end);
//
//    @Query(value = "select count(1) from liveRoom l left join teaching_area t on l.teachingAreaId = t.id where l.aid = ?1 and l.status = ?2 order by t.cameraSize desc limit ?3,?4", nativeQuery = true)
//    Long findByAreaIdAndStatusCount(String areaId, Integer status, Integer begin, Integer end);
    @Query(value = "select * from liveroom l where l.aid = ?1 and l.status = ?2 order by l.createTime desc limit ?3,?4", nativeQuery = true)
    List<LiveRoom> findByAreaIdAndStatus(String areaId, Integer status, Integer begin, Integer end);

    @Query(value = "select count(1) from liveroom l where l.aid = ?1 and l.status = ?2 ", nativeQuery = true)
    Long findByAreaIdAndStatusCount(String areaId, Integer status);


    @Async
    @Modifying
    @Transactional
    @Query("update LiveRoom l set l.liveWay = ?2,l.liveWayVal = ?4  where l.deviceSerial = ?1 and l.areaId = ?3")
    int updateLiveWay(String deviceSerial,Integer liveWay,String areaId,String liveWayVal);



//    @Column(columnDefinition = "varchar(10) default ''  comment 'ip地址'")
//    private String ipUrl;
//
//    @Column(columnDefinition = "varchar(10) default '' comment 'ip通道号'")
//    private String ipChannel;
//
//    @Column(columnDefinition = "varchar(10) default '' comment 'ip权限'")
//    private String ipAuth;
    @Query(value = "select * from liveroom l where l.ipUrl is not null and  l.ipUrl != '' " +
            " and l.ipChannel is not null and l.ipChannel != '' " +
            " and l.ipAuth is not null and l.ipAuth != '' and l.ipOnline = 1 and l.playerHeartBeat < ?1 limit ?2,?3",
        nativeQuery = true)
    List<LiveRoom> findAllDeadCameras(Date halfHourAgo, Integer begin, Integer size);

    @Query(value = "select count(*) from liveroom l where l.ipUrl is not null and  l.ipUrl != '' " +
            " and l.ipChannel is not null and l.ipChannel != '' " +
            " and l.ipAuth is not null and l.ipAuth != '' and l.ipOnline = 1 and l.playerHeartBeat < ?1 ",
            nativeQuery = true)
    Long countAllDeadCameras(Date halfHourAgo);
}
